package com.wc.AlgoOJ_train.train2_DP训练.c5_区间DP.DP0201_石子合并;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/1/13 10:07
 * @description 石子合并
 * http://43.138.190.70:8888/p/DP0201
 */
public class Main {
    static int maxN = 500 + 7;
    // 表示从dp[i][j] 表示从i到j合并所需要的最小价值
    static int[][] dp = new int[maxN][maxN];
    // 石头的代价
    static int[] stones = new int[maxN];
    // 前缀和
    static int[] sum = new int[maxN];

    static int INF = 0x3f3f3f3f;
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int n;

    public static void main(String[] args) {
        n = sc.nextInt();
        for (int i = 1; i <= n; i++) {
            stones[i] = sc.nextInt();
            sum[i] = sum[i - 1] + stones[i];
        }

        // 枚举要合并的长度
        for (int len = 2; len <= n; len++) {
            // 枚举起点
            for (int start = 1; start + len - 1 <= n; start++) {
                // 算头 算尾 终点的坐标就是start + len - 1
                int end = start + len - 1;
                // start 到 end 所需要的价值
                int val = sum[end] - sum[start - 1];
                // 设置当前区间为最大值
                dp[start][end] = INF;
                // 枚举分割点
                for (int mid = start; mid < end; mid++) {
                    dp[start][end] = Math.min(dp[start][end], dp[start][mid] + dp[mid + 1][end] + val);
                }
            }
        }
        out.println(dp[1][n]);
        out.flush();
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }
}
